package com.geostar.constructionland.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geostar.constructionland.common.entity.statistics.PostAnalyse;
import com.geostar.constructionland.statistics.mapper.PostAnalyseMapper;
import com.geostar.constructionland.statistics.service.IPostAnalyseService;
import com.geostar.constructionland.statistics.service.StatisticsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author makoto_zhu
 * @Date 2020-12-11 04:04:06
 * @Description
 **/
@Service
public class PostAnalyseServiceImpl extends ServiceImpl<PostAnalyseMapper, PostAnalyse> implements IPostAnalyseService {

    @Resource
    private PostAnalyseMapper postAnalyseMapper;

    @Resource
    private StatisticsService statisticsService;

    public IPage<PostAnalyse> getPostAnalyseForPage(int pageNo, int pageNum, String query_param_task_id) {
        IPage<PostAnalyse> analyseVersionIPage = new Page<>(pageNo, pageNum);
        LambdaQueryWrapper<PostAnalyse> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(query_param_task_id)) {
            queryWrapper.like(PostAnalyse::getUniqueTaskId, query_param_task_id);
        }
        return postAnalyseMapper.selectPage(analyseVersionIPage, queryWrapper);
    }

    @Override
    public void startPostAnalyse(String fxlx, String ywid) {
        LambdaQueryWrapper<PostAnalyse> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(fxlx)) {
            queryWrapper.like(PostAnalyse::getFxlx, fxlx);
        }
        List<PostAnalyse> postAnalyseList = postAnalyseMapper.selectList(queryWrapper);
        if(CollectionUtils.isNotEmpty(postAnalyseList)) {
            for (PostAnalyse postAnalyse : postAnalyseList) {
                statisticsService.getStatistic(postAnalyse.getUniqueTaskId(), ywid);
            }
        }
    }
}
